//
//  TSRechargeViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/19.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSRechargeViewController: TSBaseViewController {

    var tableView : UITableView!
    var headerView : TSRechargeHeaderView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configViews()
        setupNavigationTitle(title: "充值")
    }

}

extension TSRechargeViewController : TSRechargeHeaderViewDelegate {
    
    func didClickRecharge() {
        
        let vc = TSRechargeInfoViewController()
        vc.delegate = self
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension TSRechargeViewController : TSRechargeInfoViewControllerDelegate {
    func completeRecharge() {
        headerView.refreshHeaderView()
    }
}

extension TSRechargeViewController : UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 57))
        sectionView.backgroundColor = WHITE_COLOR
        
        let icon = UIView.init()
        icon.backgroundColor = BASE_RED_COLOR
        sectionView.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.bottom.equalTo(-15)
            make.size.equalTo(CGSize(width: 2, height: 13))
        }
        
        let label = UILabel.init()
        label.textColor = UIColor.hexadecimalColor(hexadecimal: "#454545")
        label.font = UIFont.systemFont(ofSize: 14)
        switch section {
        case 0:
            label.text = "充值步骤："
        case 1:
            label.text = "重要提示："
        case 2:
            label.text = "温馨提示："
        default:break
        }
        sectionView.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.centerY.equalTo(icon)
            make.left.equalTo(icon.snp.right).offset(7)
        }
        
        return sectionView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        57
    }
}

extension TSRechargeViewController : UITableViewDataSource {
//
    func numberOfSections(in tableView: UITableView) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var autoHeightCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TSAutoHeightTableViewCell.self)) as? TSAutoHeightTableViewCell
            
        if autoHeightCell == nil {
            autoHeightCell = TSAutoHeightTableViewCell.init(style: .default, reuseIdentifier: NSStringFromClass(TSAutoHeightTableViewCell.self))
        }
            
        autoHeightCell?.setupLabel(content: getInfoContent(section: indexPath.section), topInset: 0, textColor: "#A5A5A5", fontSize: 13)
        
        return autoHeightCell!
            
    }
}

extension TSRechargeViewController {
    
    func configViews() {
        
        let headerH = SCREEN_WIDTH / (375.0 / 140) + 88
        
        headerView = TSRechargeHeaderView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: headerH))
        headerView.delegate = self
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = WHITE_COLOR
        tableView.delegate = self;
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableView.automaticDimension
        tableView.tableHeaderView = headerView
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(T_NAVBARHEIGHT)
        }
        
        if #available(iOS 11, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
    }
    
    func getInfoContent(section : Int) -> String {
        
        switch section {
        case 0:
            return "1.在数字货币网购买USDT。\n2.数字货币交易网点击USDT提币、复制粘贴大潤發USDT充值地址、输入提币数量、确定。\n3.大潤發充值根据USDT区块网络速度自动到账中心钱包（折合RMB显示）。\n4.在数字货币网提现时，如有供选择方式，必须选择ERC-20提现。"
        case 1: // 暂未使用
            return "大潤發平台充值地址会不定期更新，请每次充值务必核对最新的二维码钱包地址。如因个人错误操作造成的损失，平台概不负责！"
        default:
            return ""
        }
        
    }
}
